package com.dyz.swordFingerOffer;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: daiyizheng
 * @date: 2021/5/18 20:21
 * @description:
 */
public class findRepeatNumber03 {
    public static void main(String[] args) {
        int [] arr = {5,1,3,1,4};
        findRepeatNumber03 findRepeatNumber = new findRepeatNumber03();
        findRepeatNumber.duplicate(arr);
        System.out.println(findRepeatNumber.duplicate(arr));
    }

//    // hash表
    public int duplicate (int[] numbers) {
        int res = -1;
        Set<Integer> set = new HashSet<Integer>();
        for (int i=0; i<numbers.length;i++){
            if(!set.add(numbers[i])){
                return numbers[i];
            }
        }
        return res;
    }
//public int duplicate (int[] numbers) {
//
//    int res = -1;
//    //根据数组下标，跟元素值不相等
//    int n = numbers.length;
//    if (n==0){
//        return res;
//    }
//    for(int i=0; i<n; i++){
//        while(numbers[i]!=i && numbers[numbers[i]]!=numbers[i]){
//            swap(numbers, i, numbers[i]);
//        }
//        if(numbers[i]!=i && numbers[numbers[i]]==numbers[i]){
//            return numbers[i];
//        }
//    }
//
//    return res;
//}

    public static void swap(int[] nums, int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
